﻿#include "videowindow.h"
#include "ui_videowindow.h"

#include <QDebug>
#include <QCloseEvent>

VideoWindow::VideoWindow(QSettings* settings, QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::VideoWindow),
    m_settings(settings)
{
    ui->setupUi(this);

    // 初始化
    initUI();
    init();
}

VideoWindow::~VideoWindow()
{
    // 关闭定时器
    if ( m_nTimerId != 0 )
        killTimer(m_nTimerId);

    // 清空列表，否则会发生异常
    m_playlist->clear();

    delete ui;
}

void VideoWindow::pause(bool reset)
{
    if (reset)
        m_player->setPosition(0);
    m_player->pause();

    m_playing = false;
    m_timeMsecond = 0;
    m_playDuration = 0;
}

void VideoWindow::closeEvent(QCloseEvent *event)
{
    // 不能关闭窗口
    event->ignore();
}

void VideoWindow::showEvent(QShowEvent *event)
{
    ui->gvVideo->fitInView(m_scene->sceneRect());

    QWidget::showEvent(event);
}

void VideoWindow::resizeEvent(QResizeEvent *event)
{
    ui->gvVideo->fitInView(m_scene->sceneRect());

    QWidget::resizeEvent(event);
}

void VideoWindow::timerEvent(QTimerEvent *event)
{
    Q_UNUSED(event);
    //qDebug() << "timer event, id is: " << event->timerId();

    if (m_playing) {
        m_timeMsecond += 100;
        if (m_timeMsecond > m_playDuration) {
            m_playing = false;
            m_player->pause();
        }
    }
}

void VideoWindow::initUI()
{
    // 关闭主窗口时, 退出主程序
    setAttribute(Qt::WA_QuitOnClose, false);
    // 禁用关闭按钮
    setWindowFlag(Qt::WindowCloseButtonHint, false);

    // 视频播放控件
    m_scene = new QGraphicsScene(this);
    m_scene->setSceneRect(0, 0, 800, 600);
    m_scene->setBackgroundBrush(Qt::black);
    ui->gvVideo->setFocus();
    ui->gvVideo->setScene(m_scene);
    ui->gvVideo->fitInView(m_scene->sceneRect());
    ui->gvVideo->setStyleSheet("border: none");
    m_videoItem = new QGraphicsVideoItem;
    m_videoItem->setSize(QSizeF(800, 600));
    m_videoItem->setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsFocusable);
    m_videoItem->setAspectRatioMode(Qt::IgnoreAspectRatio);
    m_scene->addItem(m_videoItem);

    // 视频播放列表
    m_playlist = new QMediaPlaylist(this);
    m_playlist->setPlaybackMode(QMediaPlaylist::CurrentItemOnce);

    // 创建视频播放器
    m_player = new QMediaPlayer(this);
    m_player->setNotifyInterval(500);
    m_player->setVideoOutput(m_videoItem);
    m_player->setPlaylist(m_playlist);

    connect(m_player, &QMediaPlayer::positionChanged, this, &VideoWindow::onVideoPositionChanged);
    connect(m_player, &QMediaPlayer::durationChanged, this, &VideoWindow::onVideoDurationChanged);
}

void VideoWindow::init()
{
    // 视频播放列表
    m_settings->beginGroup("Video");
    QString videoFileName = m_settings->value("file").toString();
    m_settings->endGroup();
    m_playlist->addMedia(QUrl::fromLocalFile(QString("%1/%2").arg(qApp->applicationDirPath(), videoFileName)));

    // 打开视频，使之处于暂停状态
    m_playlist->setCurrentIndex(0);
    m_player->pause();

    // 启动定时器
    m_nTimerId = startTimer(100);

    // 播放计时
    m_playing = false;
    m_timeMsecond = 0;
}

void VideoWindow::onVideoPositionChanged(qint64 position)
{
    Q_UNUSED(position);
}

void VideoWindow::onVideoDurationChanged(qint64 duration)
{
    Q_UNUSED(duration);
}


void VideoWindow::onPlayByPosition(const int startTime, const int duration)
{
    m_player->setPosition(startTime);
    m_player->play();

    m_playing = true;
    m_timeMsecond = 0;
    m_playDuration = duration;
}
